package com.powernode.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.reggie.common.CustomException;
import com.powernode.reggie.dto.DishDto;
import com.powernode.reggie.entity.Dish;
import com.powernode.reggie.entity.DishFlavor;
import com.powernode.reggie.mapper.DishMapper;
import com.powernode.reggie.service.DishFlavorService;
import com.powernode.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: www
 * @time: 2023/3/8 17:12
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     *  新增菜品，同时保存口味信息；
     * @param dishDto
     */
    @Override
    @Transactional   //多张表操作，需要加入事务注解
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);//菜品信息保存在Dish表中

        Long dishId = dishDto.getId();//获取菜品id 保存在dishFlavor中

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item)->{//遍历菜品口味  stream处理集合，结果赋给自己
            item.setDishId(dishId);//将dishFlavor中赋值dishId，
            return item;
        }).collect(Collectors.toList());//转成list

        dishFlavorService.saveBatch(flavors);//菜品口味保存在dishFlavor中，批量保存
    }

    /**
     * 根据id查菜品信息和口味信息，返回DishDTO对象
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);//查询基本dish信息；菜品信息

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);//拷贝dish属性到dishDto中

        //查询当前菜品对应的口味信息，从dishFlavor表中查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());//条件根据dish id查
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(flavors);//赋值

        return dishDto;
    }

    /**
     * 根据菜品信息更新菜品信息同时更细口味信息；
     * @param dishDto
     */
    @Override
    @Transactional //多张表操作，需要加入事务注解
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据 ---dish_flavor 表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        //添加当前菜品对应口味数据 ---dish_flavor 表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item)->{//遍历菜品口味  stream处理集合，结果赋给自己
            item.setDishId(dishDto.getId());//将dishFlavor中赋值dishId，
            return item;
        }).collect(Collectors.toList());//转成list

        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id删除菜品和口味信息  可以批量删除
     * @param ids
     */
    @Override
    @Transactional //操作两张表，加上事务
    public void removeWithFlavor(List<Long> ids) {

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);

        int count = this.count(queryWrapper);
        if(count > 0 ){
            throw new CustomException("菜品正在售卖，不能删除");
        }

        LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(DishFlavor::getDishId,ids);//条件根据 id查

        dishFlavorService.remove(queryWrapper1);//dish_flavor 删除
        this.removeByIds(ids);//dish 删除
    }



}
